<?php


namespace app\diy\controller;


use app\AdminRest;
use app\diy\model\DiyModel;
use app\diy\model\MenuModuleModel;
use app\diy\model\PagePathModel;
use app\overlord\info\PermissionOverlord;
use longbingcore\permissions\AdminMenu;

class ModuleController extends AdminRest
{
    public function getMoudelByKey()
    {

        //By.jingshuixian
        //2019年11月24日00:47:22
        $returnLinksData = [] ;
        //载入底部菜单数据
        $DiyMenuData = longbing_init_info_data('DiyTabbar');
        //载入模块数据
        $DiyLinkData = longbing_init_info_data('DiyLink');
        //获取模块key
        $menuKey = $this->_param['key'] ?? null;

        $menuKey = explode(',',$menuKey);

        foreach ($DiyMenuData as $menuData){
            //查询底部菜单
//            if($menuData['key'] == $menuKey ){
            if(in_array($menuData['key'],$menuKey) ){

//                dump($menuData,$DiyLinkData);exit;
                //获取底部菜单绑定的组件
                $bind_links = $menuData['bind_links'] ;
                //加载组件信息
                foreach ($DiyLinkData as $link){

                    if (in_array($link['type'] , $bind_links)){

                        $returnLinksData[] = $link ;
                    }
                }
            }
        }

        return $this->success($returnLinksData) ;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-15 15:09
     * @功能说明:功能页面
     */
    public function functionPage(){

        //功能页
        $FunctionPage = longbing_init_info_data('FunctionPage');

        $diy_id = $this->_param['diy_id'] ?? null;

        $keys  = $this->_param['key'] ?? null;
        //没有处理后台老版本和新版本数据兼容处理的问题  By.jingshuixian
        $DiyMenuData = DiyModel::where([['id', '=', $diy_id]])->find();

        $data['data'] = [];

        if(!empty($DiyMenuData['tabbar'])&&!empty($FunctionPage)){

            $DiyMenuData = json_decode($DiyMenuData['tabbar'], true);

            $key         = array_column($DiyMenuData['list'],'key');

//            $key         = array_merge($key,[1,20001]);

            $key         = array_merge($key,[1,61]);

            foreach ($FunctionPage as $value){

                $p_auth = true;

                if(isset($value['auth_model'])){

                    foreach ($value['auth_model'] as $auth){

                        $permissionClassName = 'app\\' . $auth . '\\info\\'. 'Permission'.ucfirst($auth);

                        $p = new $permissionClassName($this->_uniacid);

                        $p_auth = $p->pAuth();

                        if($p_auth==false){

                            break;
                        }

                    }

                }

                $value['level'] = !empty($value['level'])?$value['level']:1;

                if($p_auth==true&&in_array($value['key'],$key)&&($value['key']!=$keys||$value['level']!=1)){

                    $data['data'][] = $value;
                }
            }
        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 15:07
     * @功能说明:正在使用的diy
     */
    public function diyIng(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $id = DiyModel::where($dis)->value('id');

        return $this->success($id);


    }
}